package com.chuantou.yuba.ui;

import java.util.HashMap;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.chuantou.yuba.R;
import com.chuantou.yuba.api.RequestManager;
import com.chuantou.yuba.api.UriHelper;
import com.chuantou.yuba.api.request.GsonRequest;
import com.chuantou.yuba.api.to.BaseTO;
import com.chuantou.yuba.api.to.UserTO;
import com.chuantou.yuba.util.AccountManager;
import com.chuantou.yuba.util.UiUtil;

/**
 * 反馈界面
 * 
 * @author dl1001
 * 
 */
public class FeedbackActivity extends BaseActionBarActivity {
	private EditText mFeedbackView;
	private TextView mCountView;
	private ProgressDialog mProgressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feedback);
		setupViews();
		setTitle(R.string.feedback);
	}

	private void setupViews() {
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setMessage(getString(R.string.uploading));
		mProgressDialog.setCancelable(false);
		mFeedbackView = (EditText) findViewById(R.id.feedback);
		mCountView = (TextView) findViewById(R.id.count);
		mFeedbackView.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				int remainCount = 200 - mFeedbackView.getText().length();
				mCountView.setText(String.valueOf(remainCount));
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_submit, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_submit:
			if (UiUtil.isFastDoubleClick())
				return true;
			String feedback = mFeedbackView.getText().toString().trim();
			if (TextUtils.isEmpty(feedback)) {
				Toast.makeText(getApplicationContext(), R.string.input_feedback_error, Toast.LENGTH_SHORT).show();
				return true;
			}
			sendFeedback(feedback);
			return true;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void sendFeedback(String feedback) {
		AccountManager accountManager = AccountManager.getInstance(getApplicationContext());
		if (!accountManager.isLogined()) {
			UiUtil.showNoLoginDialog(this, -1, LoginActivity.ACTION_RETURN);
			return;
		}
		final UserTO user = accountManager.getUserTO();
		if (user.isExpired()) {
			Toast.makeText(this, R.string.message_token_expired, Toast.LENGTH_SHORT).show();
			LoginActivity.start(this, -1, LoginActivity.ACTION_RETURN);
			return;
		} else {
			String url = UriHelper.getFeedbackUrl();
			HashMap<String, String> params = new HashMap<String, String>();
			params.put(UriHelper.PARAMS_UID, String.valueOf(user.getUid()));
			params.put(UriHelper.PARAMS_TOKEN, user.getToken());
			params.put(UriHelper.PARAMS_MESSAGE, feedback);
			GsonRequest<BaseTO> request = new GsonRequest<BaseTO>(Method.POST, url, new Listener<BaseTO>() {

				@Override
				public void onResponse(BaseTO response) {
					if (!hasDestroyed()) {
						if (response == null) {
							Toast.makeText(getApplicationContext(), R.string.send_failed, Toast.LENGTH_SHORT).show();
						} else {
							if (response.isSuccess()) {
								Toast.makeText(getApplicationContext(), R.string.send_success, Toast.LENGTH_SHORT).show();
								onBackPressed();
							} else if (response.getStatus() == BaseTO.STATUS_TOKEN_ERROR || response.getStatus() == BaseTO.STATUS_TOKEN_EXPIRED) {
								Toast.makeText(getApplicationContext(), R.string.message_token_expired, Toast.LENGTH_SHORT).show();
								LoginActivity.start(FeedbackActivity.this, -1, LoginActivity.ACTION_RETURN);
							} else {
								String msg = response.getMsg();
								msg = TextUtils.isEmpty(msg) ? getString(R.string.send_failed) : msg;
								Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
							}
						}
					}
				}
			}, new ErrorListener() {

				@Override
				public void onErrorResponse(VolleyError error) {
					if (!hasDestroyed()) {
						Toast.makeText(getApplicationContext(), R.string.request_error, Toast.LENGTH_SHORT).show();
					}
				}
			}, params, BaseTO.class);
			Toast.makeText(getApplicationContext(), R.string.sending, Toast.LENGTH_SHORT).show();
			RequestManager.startRequest(getApplicationContext(), request);
		}
	}

}
